import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author Jiange
 * @create 2021-05-13-14:01
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        //表示等6个都好了才执行
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6,()->{
            System.out.println("最后执行");
        });
        for (int i = 0; i < 6; i++) {
            int finalI = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t"+ finalI);
                try {
                    cyclicBarrier.await();//执行之后阻塞之后的代码的运行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
